package Servlet;


import entity.Pet;
import service.impl.petServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/addPet")
public class addPet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        PrintWriter out=response.getWriter();

        petServiceImpl psi=new petServiceImpl();

        Pet pet=new Pet();
        pet.setName(request.getParameter("name"));
        pet.setSex(request.getParameter("sex"));
        pet.setBreed(request.getParameter("breed"));
        pet.setBirthday(request.getParameter("date"));
        pet.setDescRipTion(request.getParameter("Description"));

        int count=0;
        try {
            count=psi.addPet(pet);
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (count>0){
            out.println("<script type=\"text/javascript\">\n" +
                    "    alert(\"添加成功!\");\n" +
                    "    location.href=\"./index.jsp\"\n" +
                    "</script>");
        }else{
            out.println("<script type=\"text/javascript\">\n" +
                    "    alert(\"添加失败!\");\n" +
                    "    location.href=\"/addPet.jsp\"\n" +
                    "</script>");
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
